const Service = require('egg').Service;

class AnnouncementService extends Service {
  async create() {
    const { ctx } = this;
    const { title, content } = ctx.request.body;
    return await ctx.app.mysql.insert('announcements', {
      title,
      content,
    });
  }

  async delete() {
    const { ctx } = this;
    const id = ctx.params.id;
    return await ctx.app.mysql.delete('announcements', { id });;
  }

  async update() {
    const { ctx } = this;
    const id = ctx.params.id;
    const { title, content } = ctx.request.body;
    return await ctx.app.mysql.update('announcements', {
      title,
      content,
    }, {
      where: { id },
    });
  }

  async list() {
    const { ctx } = this;
    return await ctx.app.mysql.select('announcements');
  }
  
  async read() {
    const { ctx } = this;
    const id = ctx.params.id;
    return await ctx.app.mysql.get('announcements', { id });
  }
}

module.exports = AnnouncementService;
